package com.isdepci.project.factor.controller;

import java.util.List;

import com.isdepci.project.factor.utils.FactorRedisUtils;
import com.isdepci.project.factor.utils.FactorUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.isdepci.framework.aspectj.lang.annotation.Log;
import com.isdepci.framework.aspectj.lang.enums.BusinessType;
import com.isdepci.project.factor.domain.EnterpriseInfo;
import com.isdepci.project.factor.service.IEnterpriseInfoService;
import com.isdepci.framework.web.controller.BaseController;
import com.isdepci.framework.web.domain.AjaxResult;
import com.isdepci.framework.web.page.TableDataInfo;

/**
 * 企业库Controller
 *
 * @author isdepci
 * @date 2024-11-07
 */
@RestController
@RequestMapping("/factor/enterpriseInfo")
public class EnterpriseInfoController extends BaseController {
    @Autowired
    private IEnterpriseInfoService enterpriseInfoService;
    @Autowired
    private FactorUtils factorUtils;

    @GetMapping("/dict")
    public AjaxResult selectEnterpriseInfoDict() {
        List<EnterpriseInfo> list = factorUtils.selectEnterpriseInfoDict();
        return AjaxResult.success(list);
    }

    /**
     * 查询企业库列表
     */
    @PreAuthorize("@ss.hasPermi('factor:enterpriseInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(EnterpriseInfo enterpriseInfo) {
        startPage();
        List<EnterpriseInfo> list = enterpriseInfoService.selectEnterpriseInfoList(enterpriseInfo);
        return getDataTable(list);
    }

    /**
     * 获取企业库详细信息
     */
    @PreAuthorize("@ss.hasPermi('factor:enterpriseInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(enterpriseInfoService.selectEnterpriseInfoById(id));
    }

    /**
     * 新增企业库
     */
    @PreAuthorize("@ss.hasPermi('factor:enterpriseInfo:add')")
    @Log(title = "企业库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EnterpriseInfo enterpriseInfo) {
        return (enterpriseInfoService.insertEnterpriseInfo(enterpriseInfo));
    }

    /**
     * 修改企业库
     */
    @PreAuthorize("@ss.hasPermi('factor:enterpriseInfo:edit')")
    @Log(title = "企业库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EnterpriseInfo enterpriseInfo) {
        if (enterpriseInfo.getParentId().equals(enterpriseInfo.getId())){
            return AjaxResult.error("修改'" + enterpriseInfo.getEnterpriseName() + "'失败，上级分类不能是自己");
        }
        return (enterpriseInfoService.updateEnterpriseInfo(enterpriseInfo));
    }

    /**
     * 删除企业库
     */
    @PreAuthorize("@ss.hasPermi('factor:enterpriseInfo:remove')")
    @Log(title = "企业库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return  (enterpriseInfoService.deleteEnterpriseInfoById(id));
    }


    @Log(title = "企业库", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clearCache")
    public AjaxResult clearCache() {
        FactorRedisUtils.clearEnterpriseInfoListCache();
        return AjaxResult.success();
    }
}
